//
//  FXCNewTableViewController.m
//  XinLangFeiBo
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FXCNewTableViewController.h"
//#import "FXCNetWorkTool.h"
#import "SXNetworkTools.h"
#import "FXCNewsModel.h"
#import "FXCNewsCell.h"
@interface FXCNewTableViewController ()

@property (nonatomic, retain) NSMutableArray *arrayList;
@end

@implementation FXCNewTableViewController

- (void)viewWillAppear:(BOOL)animated{
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self setUpRefresh];
    
    
}

// 设置上下刷新控件
- (void)setUpRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}
//下拉刷新
- (void)loadData{
   NSString *allUrlstring = [NSString stringWithFormat:@"/nc/article/%@/0-20.html",self.urlString];

    [self loadDataForType:1 withURL:allUrlstring];
    
}
//上拉加载
- (void)loadMoreData{
    
    NSString *allUrlstring = [NSString stringWithFormat:@"/nc/article/%@/%ld-20.html",self.urlString,(self.arrayList.count - self.arrayList.count%10)];
    [self loadDataForType:2 withURL:allUrlstring];
}

- (void)loadDataForType:(int)type withURL:(NSString *)allUrlstring{
    
    [[SXNetworkTools sharedNetworkTools] GET:allUrlstring parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
//        NSLog(@"%@", dic.keyEnumerator.nextObject);
        NSString *key = dic.keyEnumerator.nextObject;
        NSArray *temArray = responseObject[key];
        NSArray *arrM = [FXCNewsModel mj_objectArrayWithKeyValuesArray:temArray];
        
        if (type == 1) {
            self.arrayList = [arrM mutableCopy];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }else if(type == 2){
            [self.arrayList addObjectsFromArray:arrM];
            
            [self.tableView.mj_footer endRefreshing];
            [self.tableView reloadData];
        }

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    

    
}
#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *ID = @"newcell";
    
    FXCNewsModel *newsModel = self.arrayList[indexPath.row];
    
    NSString *ID = [FXCNewsCell idForRow:newsModel];
    
//    if ((indexPath.row % 20 == 0)&&(indexPath.row != 0)) {
//        ID = @"NewsCell";
//    }
    
    
    
    FXCNewsCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[FXCNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    

    
    cell.newsModel = newsModel;
//    NSLog(@"%@", cell);
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FXCNewsModel *newsModel = self.arrayList[indexPath.row];
    
    CGFloat rowHeight = [FXCNewsCell heightForRow:newsModel];
    
//    if ((indexPath.row % 20 == 0)&&(indexPath.row != 0)) {
//        rowHeight = 80;
//    }
    
    return rowHeight;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
